<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Helper\Optimize;
use App\Lib\Params\BaseWebParams;
use App\Services\InfoListService;
use App\Lib\Params\InfoSearchParams;
use App\Lib\Params\WebColumnSearchParams;
use App\Model\Content;
use App\Model\Info;
use App\Model\InfoExtField;
use App\Model\InfoExtRelate;
use App\Model\WebColumn;
use App\Services\InfoParamsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class IndexController extends Controller {

//    private $_infoParams;

    public function __construct(){
//        $this->_infoParams = new InfoParamsService();
    }
	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
        $infoParams = new InfoParamsService();
        //首页轮播图
        $infoParams->getIndexImgParams();
        $indexImgInfo = Info::getInfosByParams($infoParams,array('id','index_main_img'));
        //产品推荐
        $infoParams->getIndexProductParams();
        $infos = Info::getInfosByParams($infoParams,array('id','index_main_img','title','intro'));
        $indexProductInfo = array_chunk($infos,6);
        foreach($indexProductInfo as $key=>$infos){
            foreach($infos as $infoKey=>$info) {
                $indexProductInfo[$key][$infoKey]['index_main_img'] = Optimize::getImgUrl($indexProductInfo[$key][$infoKey]['index_main_img'], '_m');
            }
        }
        //公司资讯
        $infoParams->getIndexNewsParams();
        $indexNewsInfo = Info::getInfosByParams($infoParams,array('id','index_main_img','title','create_time'));
        //解决方案
        $infoParams->getIndexSchemesParams();
        $indexSchemesInfo = Info::getInfosByParams($infoParams,array('id','index_main_img','title','intro'));
        foreach($indexSchemesInfo as $key=>$info){
            $indexSchemesInfo[$key]['index_main_img'] = Optimize::getImgUrl( $indexSchemesInfo[$key]['index_main_img'],'_m');
        }
        //成功案例
        $infoParams->getIndexCasesParams();
        $indexCasesInfo = Info::getInfosByParams($infoParams,array('id','index_main_img','title'));
        foreach($indexCasesInfo as $key=>$info){
            $indexCasesInfo[$key]['index_main_img'] = Optimize::getImgUrl( $indexCasesInfo[$key]['index_main_img'],'_m');
        }
        return View('index')
            ->with('indexImgInfo',$indexImgInfo)
            ->with('indexProductInfo',$indexProductInfo)
            ->with('indexNewsInfo',$indexNewsInfo)
            ->with('indexSchemesInfo',$indexSchemesInfo)
            ->with('indexCasesInfo',$indexCasesInfo);
	}

    public function search()
    {
        $content = Input::get('content');
        if(empty($content)){
            //跳转值首页
            Redirect::to('index');
        }
        Redirect::to('index');
    }

    private function _getListData($columnKey,$firstColumnId,$extField=false)
    {
        $columnId = Input::get('columnId',0);
        if(true === $extField){
            $extRelate = Input::get('extRelate',array());
        }else{
            $extRelate = array();
        }
        $infoListService = new InfoListService($firstColumnId,$columnId);
        $infoListService->getItems($extField);
        $infoListService->getListData($extRelate);
        return View('list')
            ->with('columnId',$columnId)
            ->with('columnKey',$columnKey)
            ->with('infoList',$infoListService->infoList)
            ->with('selectItems',$infoListService->selectItems)
            ->with('extRelate',$extRelate)
            ->with('selectDetileItems',$infoListService->selectDetileItems);
    }

    public function product()
    {
        //产品
        $firstColumnId = 2;
        $columnKey = '产品分类';
        return $this->_getListData($columnKey,$firstColumnId);
    }

    public function news()
    {
        //资讯
        $firstColumnId = 24;
        $columnKey = '资讯分类';
        return $this->_getListData($columnKey,$firstColumnId);
    }


    public function scheme()
    {
        //解决方案
        $firstColumnId = 11;
        $columnKey = '解决方案';
        return $this->_getListData($columnKey,$firstColumnId);
    }

    public function project()
    {
//        var_dump( Input::get('columnId',0));exit;
        //工程案例
        $firstColumnId = 19;
        $columnKey = '工程案例';
        $extField = true;
        return $this->_getListData($columnKey,$firstColumnId,$extField);
    }

    public function info($infoId,$pageNo=1)
    {
        $info = Info::getInfoById($infoId,array('id','title','main_img','intro','keywords','description','create_time'));
        if(is_null($info)){
            //TODO
        }

        $content = Content::getContentByInfoIdAndPageNo($infoId,$pageNo,array('id','page_no','content'));

        return View('info')
            ->with('info',$info)
            ->with('content',$content);
    }

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
